#include"BlockQueue.hpp"
#include"Task.hpp"
int data=30;
void* producer(void* arg){
    BlockQueue<Task> *bq=(BlockQueue<Task>*)arg;
    while(true){
        bq->Equeue(Task(data,data));
        std::cout<<"Producer put data:"<<data<<std::endl;
        data--;
        if(data<0)break;
        sleep(1);
    }
}
void* consumer(void* arg){
    BlockQueue<Task> *bq=(BlockQueue<Task>*)arg;
    while(true){
        Task Data=bq->Pop();
        Data.Execute();
        std::cout<<"Consumer get data:"<<Data.Result()<<std::endl;
        if(data<0)break;
    }
}
int main(){
    //申请阻塞队列
    BlockQueue<Task> *bq=new BlockQueue<Task>(5);
    //构造生产消费者
    pthread_t p[3],c[2];
    pthread_create(p,NULL,producer,bq);
    pthread_create(c,NULL,consumer,bq);
    pthread_create(c+1,NULL,consumer,bq);
    pthread_create(p+1,NULL,producer,bq);
    pthread_create(p+2,NULL,producer,bq);
    //等待生产者和消费者退出
    pthread_join(p[0],NULL);
    pthread_join(c[0],NULL);
    pthread_join(c[1],NULL);
    pthread_join(p[1],NULL);
    pthread_join(p[2],NULL);
    return 0;
}